package io.zw.modules.xinpai.service.impl;

import io.zw.common.utils.MapUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.zw.common.utils.PageUtils;
import io.zw.common.utils.Query;

import io.zw.modules.xinpai.dao.CarProjectDao;
import io.zw.modules.xinpai.entity.CarProjectEntity;
import io.zw.modules.xinpai.service.CarProjectService;


@Service("carProjectService")
public class CarProjectServiceImpl extends ServiceImpl<CarProjectDao, CarProjectEntity> implements CarProjectService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<CarProjectEntity> page = this.selectPage(
                new Query<CarProjectEntity>(params).getPage(),
                new EntityWrapper<CarProjectEntity>()
        );

        return new PageUtils(page);
    }
    @Override
    public void saveOrUpdate(String imei, List<Long> areaList) {
        //先删除关系
        this.deleteByMap(new MapUtils().put("car_id", imei));
        if(areaList == null || areaList.size() == 0){
            return ;
        }
        //保存车辆与项目区域的关系
        List<CarProjectEntity> list=new ArrayList<>(areaList.size());
        for (Long areaId : areaList) {
            CarProjectEntity userProjectEntity=new CarProjectEntity();
            userProjectEntity.setCarId(imei);
            userProjectEntity.setProjectId(areaId);
            list.add(userProjectEntity);
        }
        this.insertBatch(list);
    }
}
